package com.pwc.pluginDelay;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author fantuancyh
 * @data 2021/8/22
 * 基于插件实现延迟队列
 */
@Slf4j
@RestController
public class Provider {
    @Autowired
    RabbitTemplate rabbitTemplate;
    private static final String EXCHANGE_NAME = "delayed.exchange";
    private static final String ROUTING_KEY = "delayed.routingkey";

    @GetMapping("sendExchange/{message}/{time}")
    public void send(@PathVariable("message")String message,@PathVariable("time")String time){
        MessagePostProcessor processor = (msg)->{
            msg.getMessageProperties().setDelay(Integer.parseInt(time));
            return msg;
        };
        rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING_KEY,"延迟消息为"+message,processor);
        log.info("{}发送消息{},延迟时间为{}", new Date(),message,time);
    }

}
